package com.lxmes.sales.service.impl;

import java.util.List;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.NcMallSalesOrdersMapper;
import com.lxmes.sales.domain.NcMallSalesOrders;
import com.lxmes.sales.service.INcMallSalesOrdersService;

/**
 * 商城正式订单Service业务层处理
 *
 * @author lx
 * @date 2023-05-31
 */
@Service
public class NcMallSalesOrdersServiceImpl implements INcMallSalesOrdersService
{
    @Autowired
    private NcMallSalesOrdersMapper ncMallSalesOrdersMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询商城正式订单
     *
     * @param id 商城正式订单主键
     * @return 商城正式订单
     */
    @Override
    public NcMallSalesOrders selectNcMallSalesOrdersById(Long id)
    {
        return ncMallSalesOrdersMapper.selectNcMallSalesOrdersById(id);
    }

    /**
     * 查询商城正式订单列表
     *
     * @param ncMallSalesOrders 商城正式订单
     * @return 商城正式订单
     */
    @Override
    public List<NcMallSalesOrders> selectNcMallSalesOrdersList(NcMallSalesOrders ncMallSalesOrders)
    {
        ncMallSalesOrders.setFactoryCode(SecurityUtils.getFactoryCode());
        return ncMallSalesOrdersMapper.selectNcMallSalesOrdersList(ncMallSalesOrders);
    }

    /**
     * 新增商城正式订单
     *
     * @param ncMallSalesOrders 商城正式订单
     * @return 结果
     */
    @Override
    public int insertNcMallSalesOrders(NcMallSalesOrders ncMallSalesOrders)
    {
        ncMallSalesOrders.setCreateTime(DateUtils.getNowDate());
        return ncMallSalesOrdersMapper.insertNcMallSalesOrders(ncMallSalesOrders);
    }

    /**
     * 修改商城正式订单
     *
     * @param ncMallSalesOrders 商城正式订单
     * @return 结果
     */
    @Override
    public int updateNcMallSalesOrders(NcMallSalesOrders ncMallSalesOrders)
    {
        ncMallSalesOrders.setUpdateTime(DateUtils.getNowDate());
        return ncMallSalesOrdersMapper.updateNcMallSalesOrders(ncMallSalesOrders);
    }

    /**
     * 批量删除商城正式订单
     *
     * @param ids 需要删除的商城正式订单主键
     * @return 结果
     */
    @Override
    public int deleteNcMallSalesOrdersByIds(Long[] ids)
    {
        return ncMallSalesOrdersMapper.deleteNcMallSalesOrdersByIds(ids);
    }

    /**
     * 删除商城正式订单信息
     *
     * @param id 商城正式订单主键
     * @return 结果
     */
    @Override
    public int deleteNcMallSalesOrdersById(Long id)
    {
        return ncMallSalesOrdersMapper.deleteNcMallSalesOrdersById(id);
    }
    @Override
    public String imporIteminfo(List<NcMallSalesOrders> ncMallSalesOrdersList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncMallSalesOrdersList) || ncMallSalesOrdersList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcMallSalesOrders ncMallSalesOrders : ncMallSalesOrdersList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncMallSalesOrders);
                ncMallSalesOrders.setCreateBy(operName);
                ncMallSalesOrders.setUpdateBy(operName);
                this.insertNcMallSalesOrders(ncMallSalesOrders);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
